﻿namespace ciji.Core
{
    public class StrHelper
    {

        /// <summary>
        /// 获取枚举list
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<EnumDto> GetEnumList<T>()
        {
            var result = new List<EnumDto>();

            Type enumType = typeof(T);

            if (!enumType.IsEnum)
            {
                return result;
            }

            string[] fieldstrs = Enum.GetNames(enumType);

            foreach (var item in fieldstrs)
            {
                string description = string.Empty;
                var field = enumType.GetField(item);
                var des = field.GetCustomAttribute<DescriptionAttribute>(); //获取属性字段数组
                if (des != null)
                {
                    description = des.Description;   //属性描述
                }
                else
                {
                    description = item;  //描述不存在取字段名称
                }
                result.Add(new EnumDto
                {
                    Value = (int)Enum.Parse(enumType, item),
                    Name = description,

                });
            }

            return result;
        }

        public static List<EnumUniDto> GetEnumUniList<T>()
        {
            var result = new List<EnumUniDto>();

            Type enumType = typeof(T);

            if (!enumType.IsEnum)
            {
                return result;
            }

            string[] fieldstrs = Enum.GetNames(enumType);

            foreach (var item in fieldstrs)
            {
                string description = string.Empty;
                var field = enumType.GetField(item);
                var des = field.GetCustomAttribute<DescriptionAttribute>(); //获取属性字段数组
                if (des != null)
                {
                    description = des.Description;   //属性描述
                }
                else
                {
                    description = item;  //描述不存在取字段名称
                }
                if (!string.IsNullOrWhiteSpace(description))
                {
                    result.Add(new EnumUniDto
                    {
                        value = (int)Enum.Parse(enumType, item),
                        name = description,

                    });
                }
            }
            return result;
        }


        public static T GetEnumValueFromDescription<T>(string description) where T : struct
        {
            if (string.IsNullOrEmpty(description))
            {
                return default;
            }
            Type enumType = typeof(T);

            if (!enumType.IsEnum)
                throw new ArgumentException("T must be an enumerated type");

            foreach (T enumValue in Enum.GetValues(enumType))
            {
                if (GetDescription(enumValue).Equals(description, StringComparison.OrdinalIgnoreCase))
                {
                    return enumValue;
                }
            }
            return default;
        }
        private static string GetDescription<T>(T enumValue)
        {
            Type type = enumValue.GetType();
            if (!type.IsEnum)
                throw new ArgumentException("T must be an enumerated type");

            // Get the FieldInfo for the enum value
            FieldInfo fieldInfo = type.GetField(enumValue.ToString());

            // Get the DescriptionAttribute for the enum value
            if (Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute)) is DescriptionAttribute attribute)
            {
                return attribute.Description;
            }

            // If no DescriptionAttribute is found, return the enum value as a string
            return enumValue.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        /// <param name="target"></param>
        public static void CopyFields(object source, object target)
        {
            Type sourceType = source.GetType();
            Type targetType = target.GetType();

            // 获取 source 和 target 类型的所有字段
            FieldInfo[] sourceFields = sourceType.GetFields();
            FieldInfo[] targetFields = targetType.GetFields();

            foreach (var sourceField in sourceFields)
            {
                // 查找 target 中是否有相同名称和类型的字段
                FieldInfo targetField = Array.Find(targetFields, f => f.Name == sourceField.Name && f.FieldType == sourceField.FieldType);

                if (targetField != null)
                {
                    // 复制值
                    targetField.SetValue(target, sourceField.GetValue(source));
                }
            }
        }
    }
}